from fastapi import FastAPI
import uvicorn

from apps.app01 import app01
from apps.app02 import app02

app = FastAPI()

app.include_router(app01, prefix="/app01", tags=["第一章节：商城接口", ])
app.include_router(app02, prefix="/app02", tags=["第二章节：用户中心接口", ])

# if __name__ == '__main__':
#     uvicorn.run("main:app", host="127.0.0.1", port=8080, debug=True, reload=True)


# from typing import Union
#
# from fastapi import FastAPI
#
# app = FastAPI()
#
#
# @app.get("/get")
# def get_test():
#     return {"method": "get方法"}
#
#
# @app.post("/post")
# def post_test():
#     return {"method": "post方法"}
#
#
# @app.put("/put")
# def put_test():
#     return {"method": "put方法"}
#
#
# @app.delete("/delete")
# def delete_test():
#     return {"method": "delete方法"}


import uvicorn
from fastapi import FastAPI

from fastapi import Request
from fastapi.responses import Response
import time
from fastapi.responses import JSONResponse

# app = FastAPI()


# @app.middleware("http")
# async def m2(request: Request, call_next):
#     # 请求代码块
#     print("m2 request")
#     wlist = ['/login', '/user', '/app01/courses']
#     print(request.client.host)
#     print(request.url.path)
#     if request.url.path not in wlist:
#         return JSONResponse({
#             "code": 1000,
#             "msg": "请登录",
#         })
#     response = await call_next(request)
#     # 响应代码块
#     response.headers["author"] = "moluo"
#     print("m2 response")
#     return response


@app.get("/user")
def get_user():
    time.sleep(3)
    print("get_user函数执行")
    return {
        "user": "current user"
    }


# @app.get("/item/{item_id}")
# def get_item(item_id: int):
#     time.sleep(2)
#     print("get_item函数执行")
#     return {
#         "item_id": item_id
#     }


if __name__ == '__main__':
    uvicorn.run('main:app', host='127.0.0.1', port=8080, reload=True, workers=1)

